load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//build:STRINGER.bzl", "stringer")

go_library(
    name = "lang",
    srcs = [
        "compiler.go",
        "data_type.go",
        "doc.go",
        "expr.go",
        "gen.go",
        "parser.go",
        "scanner.go",
        ":gen-expr",  # keep
        ":gen-operator",  # keep
        ":gen-operator-stringer",  # keep
        ":gen-token-stringer",  # keep
    ],
    importpath = "github.com/cockroachdb/cockroach/pkg/sql/opt/optgen/lang",
    visibility = ["//visibility:public"],
    deps = [
        "@com_github_cockroachdb_errors//:errors",
        "@org_golang_x_text//cases",
        "@org_golang_x_text//language",
    ],
)

go_test(
    name = "lang_test",
    size = "small",
    srcs = [
        "compiler_test.go",
        "parser_test.go",
        "scanner_test.go",
    ],
    data = glob(["testdata/**"]),
    embed = [":lang"],
    deps = [
        "//pkg/testutils/datapathutils",
        "@com_github_cockroachdb_datadriven//:datadriven",
        "@com_github_cockroachdb_errors//:errors",
    ],
)

# Define the generator for the expression definitions and functions.
genrule(
    name = "gen-expr",
    srcs = ["lang.opt"],
    outs = ["expr.og.go"],
    cmd = """
      $(location //pkg/sql/opt/optgen/cmd/langgen) -out $@ exprs $(location lang.opt)
    """,
    tools = ["//pkg/sql/opt/optgen/cmd/langgen"],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)

genrule(
    name = "gen-operator",
    srcs = ["lang.opt"],
    outs = ["operator.og.go"],
    cmd = """
      $(location //pkg/sql/opt/optgen/cmd/langgen) -out $@ ops $(location lang.opt)
    """,
    tools = ["//pkg/sql/opt/optgen/cmd/langgen"],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)

stringer(
    name = "gen-token-stringer",
    src = "scanner.go",
    typ = "Token",
)

stringer(
    name = "gen-operator-stringer",
    src = "operator.og.go",
    typ = "Operator",
)
